new Vue({
  el: '#app',
  data() {
    return {
      rules: {
        Warehouse_Name: [{ required: true, message: '请输入仓库名称', trigger: 'blur' }],
        Warehouse_Type: [{ required: true, message: '请选择仓库类型', trigger: 'blur' }],
        Applicable_Type: [{ required: true, message: '请选择仓库性质', trigger: 'blur' }],
        address:[{required: true, message: '请输入详细地址', trigger: 'blur'}]
      },
      city:[],
      address:'',
      value:[],
      form: {
        Warehouse_SerialNumber: '',
        Warehouse_Name: '',
        Warehouse_Address: '',
        Warehouse_Type: '',
        Applicable_Type: '0',
        Parent_Warehouse_SerialNumber: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
      },
      warehouse:[],
      loading: false
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData(){
      const _this = this
      $.ajax({
        url: "../../../js/city.json",
        data: { action: "jQueryAjax" },
        type: "GET",
        success: function (data) {
          _this.city = data
          console.log(data);
        },
        dataType: "json"
      })
      $.ajax({
        type: 'GET',
        url: `${DomainName}/getWarehouseTypeAll?token=${$.cookie('token')}`,
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (Data) {
          _this.warehouse = Data
        },
        dataType: "json"
      });
    },
    handleType(e){
      this.form.Warehouse_Name = ""
      this.form.Warehouse_Type = ""
      this.form.Warehouse_Address = ""
      this.form.Parent_Warehouse_SerialNumber = ""
      this.value = []
      this.address  = ""
    },
    handleWarehouse(e){
      console.log(e);
      this.warehouse.forEach(item=>{
        if(item.Warehouse_SerialNumber == e){
          this.form.Warehouse_Name = item.Warehouse_Name
          this.form.Warehouse_Type = item.Warehouse_Type
          this.form.Warehouse_Address = item.Warehouse_Address
          this.value = item.Warehouse_Address.split('/').slice(0,3)
          this.address = item.Warehouse_Address.split('/')[3]
        }
      })
    },
    handleChange(e){
      this.form.Warehouse_Address = `${this.value[0]}/${this.value[1]}/${this.value[2]}/${e}`
    },
    submitForm(formName) {
      console.log(this.value);
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          $.ajax({
            url: DomainName + '/getAddAction?token=' + $.cookie("token"),
            type: 'POST',
            data: {
              Data: JSON.stringify({
                warehouse_info: [_this.form]
              })
            },
            success: function (res) {
              console.log('提交结果', res)
              _this.$message.success("提交成功")
              window.history.go(-1);
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
})